<?php

namespace iBrand\Component\Order\Listeners;

use Carbon\Carbon;
use iBrand\Component\Balance\Repository\BalanceRepository;
use iBrand\Component\Order\Models\Order;
use iBrand\Component\Order\Models\OrderItem;
use iBrand\Component\Point\Model\Point;
use iBrand\Shop\Core\Models\O2oGoods;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/11/13
 * Time: 12:07
 */
class OrderEventListener
{

    protected $balanceRepository;

    public function __construct(BalanceRepository $balanceRepository)
    {
        $this->balanceRepository = $balanceRepository;
    }

    public function onOrderCompleted(Order $order)
    {
        if (!settings('shop_get_point') OR $order->user_id == 934 OR $order->user_id == 460) {  //如果未启用积分模块，则不进行积分操作
            return;
        }

        $items = OrderItem::where(['order_id' => $order->id])->get();
        $point_total = 0;

        $note = '购物送积分';
        $double = false;
        $today = Carbon::now()->format('m');
        $year = Carbon::now()->format('Y');
        $user = $order->user;

        if ($user AND $user->birthday) {
            $birthday = date("m", strtotime($user->birthday));  //'1986-9-11',19860911
            $created = date("Y-m-d", strtotime($user->created_at));
            $orderTime = date("Y-m-d", strtotime($order->created_at));

            if (intval($birthday) == intval($today) AND $created != $orderTime) {
                $check = Point::where('note', '生日购物双倍积分')->where('user_id', $user->id)->where('created_at', 'like', '%' . $year . '%')->get()->first();
                if (!$check) {
                    $note = '生日购物双倍积分';
                    $double = true;
                }
            }
        }


        foreach ($items as $item) {
            $goods_id = $item->item_meta['detail_id'];

            $o2oGoods = O2oGoods::where('goods_id', $goods_id)->where('shop_id', $order->channel_id)->first();

            if (!$o2oGoods->can_get_point) {
                continue;
            }

            $point = $item->getPoint();
            if (!$point) {
                continue;
            }

            if ($double) {
                $point = $point * 2;
            }


            if (!Point::where('action', 'order_item')->where('item_id', $item->id)->first()) {
                Point::create([
                    'user_id' => $order->user_id,
                    'action' => 'order_item',
                    'note' => $note,
                    'item_type' => 'ElementVip\Component\Order\Models\OrderItem',
                    'item_id' => $item->id,
                    'value' => $point,
                    'status' => 1]);
            }
            $point_total += $point;
        }

        event('point.change', [$order->user_id]);
    }

    public function nationalDay(Order $order)
    {
        $now = Carbon::now();
        if ('2019-10-01 00:00:00' > $now OR $now > '2019-10-08 00:00:00') {
            return;
        }

        $reword = floor(($order->total / 100) / 188);

        if ($reword >= 1) {
            $value = $reword * 10.1 * 100;
            $balance = $this->balanceRepository->getSum($order->user_id);
            $this->balanceRepository->addRecord([
                'user_id' => $order->user_id,
                'type' => 'reword',
                'note' => '国庆购物返现',
                'value' => $value,
                'current_balance' => $balance + $value,
                'origin_id' => $order->id,
                'origin_type' => Order::class,
            ]);
        }

    }

    public function subscribe($events)
    {
        $events->listen(
            'o2o.order.onOrderCompleted',
            'iBrand\Component\Order\Listeners\OrderEventListener@onOrderCompleted'
        );

        $events->listen(
            'o2o.order.activities',
            'iBrand\Component\Order\Listeners\OrderEventListener@nationalDay'
        );

    }

}